package com.yonyougov.devops.cruxcisupport.kubeconfig;

import com.yonyougov.devops.cruxcisupport.kubeconfig.dto.KubeConfigQueryRequestDto;
import com.yonyougov.devops.cruxcisupport.kubeconfig.dto.KubeConfigSaveRequestDto;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
* Created by ligson on 2017/8/11.
*/
@RestController
@RequestMapping("/api/v2/kubeconfig")
public class KubeConfigController extends CrudController<KubeConfigEntity, BigInteger> {

    @Autowired
    private KubeConfigService kubeConfigService;

    @Override
    public BaseService<KubeConfigEntity, BigInteger> getService() {
        return kubeConfigService;
    }

    //<!--用户自定义代码开始-->
    @ApiOperation(value = "模糊+分页查询", notes = "模糊+分页查询")
    @PostMapping("/kc/page/like")
    public BaseResponseDto findKubeConfigPageLike(@RequestBody @Valid KubeConfigQueryRequestDto request) {
        KubeConfigEntity build = KubeConfigEntity.builder().build();
        BeanUtils.copyProperties(request,build);

        Pagination<KubeConfigEntity> pagination = kubeConfigService.findByKubeConfigPageLike(build);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "kubeconfig保存或新增", notes = "kubeconfig保存或新增")
    @PostMapping("/kc/save")
    public BaseResponseDto save(@RequestBody @Valid KubeConfigSaveRequestDto request) {
        KubeConfigEntity build = KubeConfigEntity.builder().build();
        BeanUtils.copyProperties(request,build);

        kubeConfigService.save(build);

        return BaseResponseDto.getSuccessInstance();
    }


    //<!--用户自定义代码结束-->
}
